package shotgun.my.sweetutil;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import shotgun.my.sweetutil.http.HttpClientUtils;

import java.text.SimpleDateFormat;

/**
 * @author wulingming
 */
public class RemoteConfigUtil {

    public static JSONObject getJsonConfigFromRemote(String... urls) {

        for (String url : urls) {
            try {
                String result = HttpClientUtils.HTTP_CLIENT_OK.execute("GET", url, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
                        "Accept-Encoding: gzip, deflate, br\n" +
                        "Accept-Language: zh-CN,zh;q=0.9\n" +
                        "Cache-Control: max-age=0\n" +
                        "Connection: keep-alive\n" +
                        "sec-ch-ua: \"Chromium\";v=\"21\", \" Not;A Brand\";v=\"99\"\n" +
                        "sec-ch-ua-mobile: ?0\n" +
                        "sec-ch-ua-platform: \"Windows\"\n" +
                        "Sec-Fetch-Dest: document\n" +
                        "Sec-Fetch-Mode: navigate\n" +
                        "Sec-Fetch-Site: none\n" +
                        "Upgrade-Insecure-Requests: 1\n" +
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36", "");
//                System.out.println("result=" + result);
                if (result != null && !result.isEmpty()) {
                    System.out.println("获取到远程配置，from url=" + url);
                    return JSON.parseObject(result);
                }
            } catch (Exception e) {
                System.out.println("获取远程配置异常，更换下一个远程配置");
            }
        }
        return null;
//        throw new IllegalArgumentException("未获取到远程配置！！！！！！");
    }

    public static void main2(String[] args) {

        String cnblog = "https://files-cdn.cnblogs.com/files/wulm/PubKeys.json?t=" + System.currentTimeMillis();
        String gitee = "https://gitee.com/a592622272/key-house/raw/master/PubKeys.json";


        JSONObject result = getJsonConfigFromRemote(cnblog, gitee);

        System.out.println(result.get("mykey"));

    }

    public static void main(String[] args) {

        String taobao = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
        String suning = "http://quan.suning.com/getSysTime.do";
        String uukit = "http://api.uukit.com/time";
        String nowApi = "http://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
        //也可以通过请求页面，查看response中date解析。

        long curTime = getTimeStampFromRemote(taobao, suning, uukit, nowApi);

        System.out.println("毫秒时间戳：" + curTime);

    }

    public static long getTimeStampFromRemote(String... urls) {

        for (String url : urls) {
            try {
                String result = HttpClientUtils.HTTP_CLIENT_OK.execute("GET", url, "Accept: application/json,text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
                        "Accept-Encoding: gzip, deflate, br\n" +
                        "Accept-Language: zh-CN,zh;q=0.9\n" +
                        "Connection: keep-alive\n" +
                        "sec-ch-ua: \"Chromium\";v=\"21\", \" Not;A Brand\";v=\"99\"\n" +
                        "sec-ch-ua-mobile: ?0\n" +
                        "sec-ch-ua-platform: \"Windows\"\n" +
                        "Sec-Fetch-Dest: document\n" +
                        "Sec-Fetch-Mode: navigate\n" +
                        "Sec-Fetch-Site: none\n" +
                        "Upgrade-Insecure-Requests: 1\n" +
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36", "");
                if (result != null && !result.isEmpty()) {
                    System.out.println("获取到远程配置，from url=" + url);
                    System.out.println("result=" + result);

                    if (url.contains("taobao.com")) {
                        //淘宝
                        return JSON.parseObject(result).getJSONObject("data").getLong("t");
                    } else if (url.contains("suning.com")) {
                        //苏宁
                        String sysTime2 = JSON.parseObject(result).getString("sysTime2").substring(0, 10);
                        return new SimpleDateFormat("yyyy-MM-dd").parse(sysTime2).getTime();
                    } else if (url.contains("uukit.com")) {
                        //uukit
                        return JSON.parseObject(result).getJSONObject("data").getLong("timestamp") * 1000;
                    } else if (url.contains("k780.com")) {
                        //nowApi
                        return JSON.parseObject(result).getJSONObject("result").getLong("timestamp") * 1000;
                    } else {
                        throw new IllegalArgumentException("没有匹配到url对应解析规则！！！url=" + url);
                    }

                } else {
                    System.out.println("数据返回为空，更换下一个远程配置，from url=" + url);
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("获取远程配置异常，更换下一个远程配置");
            }
        }
        System.out.println("都没有获取到网络时间，使用本地时间");
        return System.currentTimeMillis();
    }

}
